Skip to content

Keep out CAs from cert manager if allowed only by callback - #11110

Open
mattia-moffa wants to merge 20 commits into
wolfSSL:masterfrom
mattia-moffa:20260806-ca-trust-sslctx
Open

mattia-moffa wants to merge 20 commits into
wolfSSL:masterfrom
mattia-moffa:20260806-ca-trust-sslctx

Conversation

@mattia-moffa

@mattia-moffa mattia-moffa commented Aug 7, 2026 •

Copy link
Copy Markdown
Member

Description

When an application's verify callback overrides errors on a chain CA cert, DoVerifyCallback() clears ret. ProcessPeerCerts() then reads ret==0 as proof that the CA is valid and calls AddCA() to add it to the WOLFSSL_CTX certificate manager.

Instead, the callback allowing a CA is meant to be a one-time exception, specific to that one peer (i.e. the CA shouldn't go to the CTX's cert manager permanently). This fix keeps such a CA on a list owned by that Certificate message. It is still used to verify the rest of the chain, but it never enters the certificate manager, and the list is freed once the message is done.

Fixes zd#22289

Testing

./configure && make && make check

@mattia-moffa mattia-moffa self-assigned this Aug 7, 2026
Copilot AI lite review requested due to automatic review settings August 7, 2026 20:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adjusts certificate-chain processing so that intermediate CA certificates are not added to the WOLFSSL_CTX certificate manager when they are only accepted due to an application verify-callback overriding ASN_NO_SIGNER_E or ASN_SELF_SIGNED_E. This preserves the intended “one-time exception” semantics of the callback and prevents unintended trust persistence across connections.

Changes:

  • Track the pre-callback verification result (preCbRet) in ProcessPeerCerts() before invoking DoVerifyCallback().
  • When the callback clears ASN_NO_SIGNER_E / ASN_SELF_SIGNED_E to success, set skipAddCA so the CA is not added to the cert manager.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/internal.c Resolved
@mattia-moffa

Copy link
Copy Markdown
Member Author

Jenkins retest this please

@github-actions

github-actions Bot commented Aug 8, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

gcc-arm-cortex-m0plus

  • FLASH: .text +228 B (+0.3%, 72,075 B / 262,144 B, total: 27% used)

gcc-arm-cortex-m3

  • FLASH: .text +768 B (+0.6%, 133,641 B / 262,144 B, total: 51% used)

gcc-arm-cortex-m4

  • FLASH: .text +768 B (+0.4%, 210,984 B / 262,144 B, total: 80% used)

gcc-arm-cortex-m4-baremetal

  • FLASH: .text +256 B (+0.3%, 74,927 B / 262,144 B, total: 29% used)

gcc-arm-cortex-m4-crypto-only

  • FLASH: .text +256 B (+0.1%, 183,755 B / 262,144 B, total: 70% used)

gcc-arm-cortex-m4-dtls13

  • FLASH: .text +768 B (+0.4%, 198,688 B / 1,048,576 B, total: 19% used)

gcc-arm-cortex-m4-min-ecc

  • FLASH: .text +256 B (+0.4%, 69,393 B / 262,144 B, total: 26% used)

gcc-arm-cortex-m4-openssl-compat

  • FLASH: .text +1,152 B (+0.1%, 807,556 B / 1,048,576 B, total: 77% used)

gcc-arm-cortex-m4-pkcs7

  • FLASH: .text +256 B (+0.1%, 224,972 B / 262,144 B, total: 86% used)

gcc-arm-cortex-m4-pq

  • FLASH: .text +768 B (+0.2%, 316,848 B / 1,048,576 B, total: 30% used)

gcc-arm-cortex-m4-rsa-only

  • FLASH: .text +1,088 B (+0.3%, 347,368 B / 1,048,576 B, total: 33% used)

gcc-arm-cortex-m4-sp-math

  • FLASH: .text +256 B (+0.4%, 69,393 B / 262,144 B, total: 26% used)

gcc-arm-cortex-m4-tls12

  • FLASH: .text +768 B (+0.6%, 134,409 B / 262,144 B, total: 51% used)

gcc-arm-cortex-m4-tls13

  • FLASH: .text +768 B (+0.3%, 253,570 B / 262,144 B, total: 97% used)

gcc-arm-cortex-m7

  • FLASH: .text +768 B (+0.4%, 210,984 B / 262,144 B, total: 80% used)

gcc-arm-cortex-m7-pq

  • FLASH: .text +832 B (+0.3%, 317,744 B / 1,048,576 B, total: 30% used)

gcc-arm-cortex-m7-tls13

  • FLASH: .text +768 B (+0.3%, 253,570 B / 262,144 B, total: 97% used)

stm32-sim-stm32h753

  • FLASH: .text +192 B (+0.1%, 209,380 B / 2,097,152 B, total: 10% used)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

@mattia-moffa
mattia-moffa force-pushed the 20260806-ca-trust-sslctx branch from 6940aa6 to 6bb6321 Compare August 17, 2026 20:56
@mattia-moffa
mattia-moffa force-pushed the 20260806-ca-trust-sslctx branch 2 times, most recently from cf1d587 to f58f494 Compare September 16, 2026 17:55
@mattia-moffa mattia-moffa added the For This Release Release version 5.9.4 label Sep 16, 2026

@wolfSSL-Fenrir-bot wolfSSL-Fenrir-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fenrir Automated Review — PR #11110

Scan targets checked: wolfcrypt-src, wolfcrypt-bugs, wolfssl-src, wolfssl-bugs

Findings: 2
2 finding(s) posted as inline comments (see file-level comments below)

This review was generated automatically by Fenrir. Reported findings require changes before merge.

Comment thread wolfcrypt/src/asn.c Resolved
Comment thread src/internal.c Resolved
@mattia-moffa

Copy link
Copy Markdown
Member Author

Jenkins retest this please

Comment thread src/internal.c Resolved
Comment thread src/internal.c Resolved
@JacobBarthelmeh JacobBarthelmeh removed the For This Release Release version 5.9.4 label Sep 17, 2026
@mattia-moffa
mattia-moffa force-pushed the 20260806-ca-trust-sslctx branch from d53f9a8 to 182289d Compare October 14, 2026 02:46

@wolfSSL-Fenrir-bot wolfSSL-Fenrir-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fenrir Automated Review — PR #11110

Scan targets checked: wolfcrypt-src, wolfcrypt-bugs, wolfssl-src, wolfssl-bugs
Coverage: 5 of 7 in-scope changed file(s) opened by the reviewer; not opened: wolfssl/internal.h, wolfssl/ssl.h

Findings: 4
4 finding(s) posted as inline comments (see file-level comments below)

This review was generated automatically by Fenrir. Reported findings require changes before merge.

Review tier: Max

Comment thread wolfcrypt/src/asn.c Resolved
Comment thread wolfcrypt/src/asn.c Resolved
Comment thread src/internal.c Resolved
Comment thread src/internal.c Resolved

@wolfSSL-Fenrir-bot wolfSSL-Fenrir-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fenrir Automated Review — PR #11110

Scan targets checked: wolfcrypt-src, wolfcrypt-bugs, wolfssl-src, wolfssl-bugs
Coverage: 3 of 4 in-scope changed file(s) opened by the reviewer; not opened: wolfssl/internal.h

Findings: 1
1 finding(s) posted as inline comments (see file-level comments below)

This review was generated automatically by Fenrir. Reported findings require changes before merge.

Review tier: Lite

Comment thread src/internal.c
/* Do verify callback */
preCbRet = ret;
ret = DoVerifyCallbackHeldCrl(ssl, args, ret, &heldCrlRet);
if (ret == 0 && preCbRet != 0)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CA whose held CRL error was waived by the verify callback is still added to the shared CertManager · Verify-callback override semantics

The CRL error is held aside in heldCrlRet and ret is set to 0 before the callback runs, so preCbRet is 0. When DoVerifyCallbackHeldCrl waives that error, it clears heldCrlRet and returns 0, so caIsTemp stays 0. The CA then goes through AddCA into the CertManager shared by every connection on the CTX, which is the leak this PR is meant to close.

Suggested fix: Save heldCrlRet before calling DoVerifyCallbackHeldCrl, and set caIsTemp when it was nonzero but the call cleared it and returned 0.

Related known findings (similar but distinct; listed for context, not part of this finding)

  • F-14457 (assigned): File/function: both are in the ProcessPeerCerts chain-CA loop, where the callback result gates AddCA. The line numbers differ because the PR shifted the code. Operation: F-14457 is the general case where a callback-cleared error lets AddCA(SSL_CM) run. The candidate is narrower. It is the held-CRL path, where ret is already 0 before the callback runs, so preCbRet is 0. DoVerifyCallbackHeldCrl then clears heldCrlRet, so caIsTemp is never set. That path exists in the PR's fix and is not i

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment is accurate and worth fixing for consistency (setting caIsTemp whenever heldCrlRet was nonzero going in might be the simpler change).

@JacobBarthelmeh JacobBarthelmeh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reviewed the changes. They look good, the only adjustment I'd request is the bot's comment about consistency of keeping a CA as temporary if the verify callback overrides the CRL error. I reviewed that comment and believe it is accurate.

- A chain could become longer than the waived CA's path length; the
  limit was being lost and silently replaced with the maximum
- A hash used to authorize delegated OCSP responders was left blank on
  waived CAs
- OCSP responses signed by a waived CA no longer fail to verify
- The callback is no longer asked again for every cert below a waived CA
  in WOLFSSL_SMALL_CERT_VERIFY builds
- Unknown-extension callback is now called on waived CAs with an unknown
  critical extension
- Changed waived CA list lifetime to last for the whole connection
  rather than the Certificate message, so it's still there when a
  stapled OCSP status arrives
The lookup that searches an OCSP responder's extra signer list was
compiled only when OCSP stapling v2 was enabled, while it's now needed
for the certs accepted via the verify callback.
@padelsbach

Copy link
Copy Markdown
Contributor

jenkins retest this please

@Frauschi Frauschi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix works and the new test fails without it; the main gaps are CRL verification against waived CAs and the list never being reset between handshakes. Also, the PR description and the WOLFSSL_TEMP_CA comment in ssl.h still say the list is scoped to one Certificate message, but it now lives for the whole connection.

Comment thread src/internal.c Resolved
Comment thread src/ocsp.c Resolved
Comment thread src/internal.c Resolved
Comment thread wolfcrypt/src/asn.c Resolved
Comment thread src/internal.c Resolved
Comment thread tests/api.c Resolved
Comment thread src/internal.c Resolved
Comment thread src/internal.c Resolved
Comment thread src/internal.c Resolved
- Pass the connection's waived CA list down the handshake CRL checks to
  VerifyCRLE;
- Don't put a CRL entry's verdict into the cert manager when a CA waived
  by the callback verified it;
- Carry the waived CA list on WOLFSSL_X509_STORE_CTX so an
  X509_verify_cert() from the callback resolves the waived CAs too;
- Clear it in X509_STORE_CTX_init so a context built by the application
  only verifies against the store;
- Check in test_wolfSSL_X509_STORE_CTX_set0_crls_handshake that the
  chain CAs go into the waived CA list rather than to the certificate
  store.
If an OCSP response is signed by a CA waived by the certificate
callback, and then it is added to the ctx's certificate manager, it will
be trusted by other connections too, but these connections don't trust
the signer. So, don't add that OCSP response to the cert manager in this
case.
This is needed so that renegotiation and post-handshake auth don't
inherit signers from the previous Certificate message.

Also prevents duplicate waived CAs by checking before adding.
Move the extra signer (waived CA) lookup in ParseCertRelative after
every cert manager lookup, so a trusted CA has priority over an
untrusted one that was accepted by the verify callback.
Install the certificate manager's word32 callback alongside the word16
one, as AddCA() does, so an intermediate whose critical extension only
the word32 callback accepts no longer fails the handshake.
- Waive only the missing trust anchor, so the handshake succeeds only
  when the chain CAs act as signers;
- Check that the CA the callback waived is not usable as a signer
  afterwards either;
- Add a test case for wolfSSL_clear() that verifies that a reused
  WOLFSSL object doesn't trust the previous peer's CAs;
- Add a test case for a stapled OCSP response whose responder is one of
  those CAs.
Look up the issuer of a signer being built in the CSRv2 pending pool too
(not just in the connection's waived CA list), so the path length
imposed by the issuer isn't lost when it's only in the pending pool.
@mattia-moffa
mattia-moffa force-pushed the 20260806-ca-trust-sslctx branch from 265002f to e3e0145 Compare November 17, 2026 23:53
- Guard the wolfSSL_clear() test with !WOLFSSL_NO_TLS12
- Skip the stapled OCSP test when WOLFSSL_ALT_CERT_CHAINS is defined,
  because it clears the CA error before the callback runs
@mattia-moffa
mattia-moffa force-pushed the 20260806-ca-trust-sslctx branch from 4f600f1 to 63bb22e Compare November 18, 2026 03:56

@Frauschi Frauschi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All points from the last round are addressed.

Comment thread src/crl.c
#else /* NO_SKID */
ca = GetCA(cm, crle->issuerHash);
#endif /* NO_SKID */
if (ca == NULL && extraCa != NULL) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing in the suite covers a CRL issued by a waived CA. A memio case CRL-checking server-chain.pem against certs/crl/ca-int.pem and ca-int2.pem would lock this in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants